<?php
require_once APPLICATION_PATH.'/models/Article.php';
require_once APPLICATION_PATH.'/models/Cate.php';
require_once 'BaseController.php';
class ArticleController extends BaseController{

    public function listAction(){
        $tblart = new Article();
        $this->view->articles = $tblart->getArticles();
        $this->view->tblcate = new Cate();
        $this->view->filterid = 0;
    }

    public function filterAction(){
        $tblart = new Article();
        $cateid = $this->_getParam('cateid');
        if(!is_numeric($cateid)){
            //参数不合法
            header('Location:'.ADMIN_PATH.'/Article/list');
            exit;
        }
        if($cateid == 0) $this->view->articles = $tblart->getArticles();
        else $this->view->articles = $tblart->getArticlesByCate($cateid);
        $this->view->tblcate = new Cate();
        $this->view->filterid = $cateid;

        $this->render('list');
    }

    public function editAction(){
        $aid = $this->getCheckedId();
        $tblart = new Article();
        $tblcate = new Cate();
        if(($this->view->article = $tblart->getArticleById($aid)) == null){
            //分类不存在
            header('Location:'.ADMIN_PATH.'/Article/list');
            exit;
        }
        $this->view->cates = $tblcate->getCates();
        $this->view->act = 'edit';
    }

    public function addAction(){
        $tblcate = new Cate();
        $this->view->article = array(
            'id' => '',
            'title' => '',
            'content' => '',
            'cateid' => 0,
        );
        $this->view->cates = $tblcate->getCates();
        $this->view->act = 'add';

        $this->render('edit');
    }

    public function topAction(){
        $aid = $this->getCheckedId();
        $tblart = new Article();
        $set = array(
            'posttime' => time(),
        );

        if($tblart->updateart($this->getCheckedId(), $set)!=1){
            //失败
        }else {
            //成功
        }
        header('Location:'.ADMIN_PATH.'/Article/list');
        exit;
    }

    public function delAction(){
        $aid = $this->getCheckedId();
        $tblart = new Article();

        if($tblart->deleteart($aid)<1){
            //删除失败
        }else {
            //删除成功，更新cate->artcount
            $tblcate = new Cate();
            $tblcate->updateArtCount();
        }
        header('Location:'.ADMIN_PATH.'/Article/list');
        exit;
    }

    public function doupdateAction(){
        $act = $this->_getParam('act');
        $tblart = new Article();
        $set = array(
            'title' => $this->_getParam('title'),
            'cateid' => $this->_getParam('cateid'),
            'content' => $this->_getParam('content'),
        );

        if($act == 'edit'){
            $affected = $tblart->updateart($this->getCheckedId(), $set);
            if($affected == 0){
                echo '更新无效：您没有对内容进行任何更改！';
            }else if($affected == 1){
                echo '更新成功！';
            }else{
                echo '更新失败！请认真检查您所填写的内容！';
            }
        }
        else if($act == 'add'){
            $set['posttime'] = time();
            if($tblart->addart($set)<1){
                //添加失败
            }else {
                //添加成功，更新cate->artcount

            }
        }
        else{}

        $tblcate = new Cate();
        $tblcate->updateArtCount();
        header('Location:'.ADMIN_PATH.'/Article/list');
        exit;
    }

    private function getCheckedId(){
        $id = $this->_getParam('id');
        if(!is_numeric($id)){
            //参数不合法
            header('Location:'.ADMIN_PATH.'/Article/list');
            exit;
        }
        return $id;
    }

}
